﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DataContract(Name="toolBarSet", Namespace = "")]
    [KnownType(typeof(ToolBarItem))]
    public class ToolBarSet : IStateManager
    {
        #region Fields

        bool defaultValue = false;
        private bool isTrackingViewStateValue;
        private ToolBarItemCollection itemsValue = null;
        private StateBag viewStateValue;

        #endregion Fields

        #region Constructors

        public ToolBarSet()
            : this(false, null)
        {
            // overloaded constructor
        }

        public ToolBarSet(bool newLine, ToolBarItemCollection tbic)
        {
            this.BreakInNewLine = newLine;
            this.itemsValue = tbic;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets/Sets a boolean value indicating whether the ToolBarSet should be forced to break in a new line or 
        /// continue adjacent to the ToolBarSet before it in the same line.
        /// </summary>
        [IgnoreDataMember,
        Bindable(false),
        Category("Behavior"),
        DefaultValue(false),
        NotifyParentProperty(true),
        Description("if true, puts the toolBar in a newline")]
        public bool BreakInNewLine
        {
            get
            {
                object o = (object)ViewState["BreakInNewLine"];
                return (o == null) ? false : (bool)o;
            }
            set
            {
                defaultValue = false;
                ViewState["BreakInNewLine"] = value;
            }
        }

        /// <summary>
        /// A collection of ToolBarItems. This collection represents every button seen inside a ToolBarSet
        /// </summary>
        [DataMember(Name="items",Order=2),
        Category("Behavior"),
        Description("A collection of ToolBarItem's "),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Editor(typeof(Typps.ToolBarItemEditor), typeof(System.Drawing.Design.UITypeEditor)),
        PersistenceMode(PersistenceMode.InnerProperty)]
        public ToolBarItemCollection Items
        {
            get
            {
                if (itemsValue == null)
                    itemsValue = new ToolBarItemCollection();
                if (isTrackingViewStateValue)
                {
                    ((IStateManager)(itemsValue)).TrackViewState();
                }
                return itemsValue;
            }
        }

        /// <summary>
        /// The image to display in the left corner of the of the toolbarset. This is optional. Default value is an empty string.
        /// </summary>
        [DataMember(Name="leftBorderImage",Order=0),
        Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor)),
        DefaultValue(""),
        NotifyParentProperty(true)]
        public string LeftBorderImage
        {
            get
            {
                return (ViewState["LeftBorderImage"] == null) ?
                    string.Empty : (string)ViewState["LeftBorderImage"];
            }
            set
            {
                defaultValue = false;
                ViewState["LeftBorderImage"] = value;
            }
        }

        /// <summary>
        /// The icon to show in the toolbar for the more options command. This is only valid when the editor IconsMode is set to Icons (the default).
        /// </summary>
        [DataMember(Name="optionsImage",Order=1),
        Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor)),
        DefaultValue("toolBarset-options.gif"),
        NotifyParentProperty(true)]
        public string OptionsImage
        {
            get
            {
                return (ViewState["OptionsImage"] == null) ?
                    "toolBarset-options.gif" : (string)ViewState["OptionsImage"];
            }
            set
            {
                defaultValue = false;
                ViewState["OptionsImage"] = value;
            }
        }

        /// <summary>
        /// Enables the little dropdown Icon in a ToolBarSet. You must provide an argument to this property that will help you
        /// distinguish one option from another as a single event handler is fired for all options clicked. You can have 1 option per toolbar set.
        /// </summary>
        [DataMember(Name = "optionsName", Order = 2),
        NotifyParentProperty(true),
        DefaultValue("")]
        public string OptionsName
        {
            get
            {
                return (string)(ViewState["OptionsName"] ?? string.Empty);
            }
            set
            {
                ViewState["OptionsName"] = value;
            }
        }

        /// <summary>
        /// Gets or sets a tooltip for the ToolBarOptions icon. The default is an empty string.
        /// </summary>
        [DataMember(Name = "optionsTitle"),
        NotifyParentProperty(true),
        DefaultValue("")]
        public string OptionsToolTip
        {
            get
            {
                object o = (object)ViewState["OptionsToolTip"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["OptionsToolTip"] = value;
            }
        }

        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return isTrackingViewStateValue;
            }
        }

        internal bool Defaults
        {
            get
            {
                return defaultValue;
            }
            set
            {
                defaultValue = value;
            }
        }

        private StateBag ViewState
        {
            get
            {
                if (viewStateValue == null)
                {
                    viewStateValue = new StateBag(false);
                    if (isTrackingViewStateValue)
                    {
                        ((IStateManager)viewStateValue).TrackViewState();
                    }
                }
                return viewStateValue;
            }
        }

        #endregion Properties

        #region Methods

        void IStateManager.LoadViewState(object savedState)
        {
            this.LoadViewState(savedState);
        }

        object IStateManager.SaveViewState()
        {
            if (Defaults)
                return null;
            return this.SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            isTrackingViewStateValue = true;
            if (viewStateValue != null)
                ((IStateManager)viewStateValue).TrackViewState();
            if (itemsValue != null)
                ((IStateManager)itemsValue).TrackViewState();
        }

        internal void LoadViewState(object savedState)
        {
            object[] states = (object[])savedState;
            if (states != null)
            {
                ((IStateManager)ViewState).LoadViewState(states[0]);
                ((IStateManager)Items).LoadViewState(states[1]);
            }
        }

        internal object SaveViewState()
        {
            object[] states = new object[2];
            states[0] = (viewStateValue != null) ? ((IStateManager)viewStateValue).SaveViewState() : null;
            states[1] = (itemsValue != null) ? ((IStateManager)itemsValue).SaveViewState() : null;
            return states;
        }

        internal void SetDirty()
        {
            if (viewStateValue != null)
                viewStateValue.SetDirty(true);
            if (itemsValue != null)
                itemsValue.SetDirty();
        }

        #endregion Methods
    }
}